package com.xqls.biz.impl;

import com.xqls.biz.FluggasBlockAdministrationBiz;
import com.xqls.dal.model.FluggasBlockAdministration;
import com.xqls.service.FluggasBlockAdministrationService;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hdg on 2017/7/5.
 */
@Service
public class FluggasBlockAdministrationBizImpl implements FluggasBlockAdministrationBiz {
    private static Logger logger = Logger.getLogger(FluggasBlockAdministrationBiz.class);
    @Autowired
    FluggasBlockAdministrationService FluggasBlockAdministrationService;

    @Override
    public List<String> BlockAdministration(Map<String,Object> map) {
        Map<String,Object> blockmap = new HashMap<>();
        String blockUrl = map.get("blockUrl").toString();
        blockUrl = blockUrl.substring(0, blockUrl.length() - 1);
        int blockId = Integer.parseInt(blockUrl);
        map.put("blockUrl",blockUrl);
        blockmap.put("blockId", blockId);
        List<FluggasBlockAdministration> FluggasBlockList =FluggasBlockAdministrationService.selectByblock(blockmap);
        String displayfield=null;
        List<String> fieldList=new ArrayList<String>();
        if(FluggasBlockList.size()==0||map.get("blockUrl").equals("1;")){
            blockmap.put("blockUrl","1;");
            blockmap.put("blockId",1);
            FluggasBlockList=FluggasBlockAdministrationService.selectByblock(blockmap);
        }
        for(int i=0;i<FluggasBlockList.size();i++){
            displayfield=FluggasBlockList.get(i).getDisplayfield();
        }
        String[] str=displayfield.split(",");
        for (int i=0;i<str.length;i++){
            fieldList.add(str[i]);
        }
        return fieldList;
    }
    @Override
    public CommonResult selectBlockAdministration(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult commonResult = new CommonResult();
        try {

            commonPage.setPageData(FluggasBlockAdministrationService.selectAll(commonPage.pageToMap()));
            Map<String,Object> showMap=new HashMap<String,Object>();
            showMap.put("fluggasadministrationList",commonPage.getPageData());
            commonPage.setHtml(VelocityUtil.getHtmlByMap("fluggasadministration/fluggasadministrationListData.vm",showMap,request,response,request.getSession().getServletContext()));
            commonResult.setIsSuccess(true);
        } catch (Exception e) {
            logger.error("动态操作列表异常", e);
        }
        return commonResult;
    }
    @Override
    public CommonResult updateFluggasBlockAdministration(FluggasBlockAdministration fluggasBlockAdministration) {
        CommonResult commonResult = new CommonResult();
        boolean success = false;
        try {
            success = FluggasBlockAdministrationService.updateByPrimaryKey(fluggasBlockAdministration)>0;
        } catch (Exception e) {
            logger.error("动态指标编辑异常", e);
        }
        commonResult.setIsSuccess(success);
        return commonResult;
    }

    @Override
    public FluggasBlockAdministration queryFluggasBlockAdministratiobByfluggas(Integer fluggasId) {
        return FluggasBlockAdministrationService.selectByPrimaryKey(fluggasId);
    }

    @Override
    public CommonResult deleteByList(List<Integer> idList) {
        CommonResult commonResult = new CommonResult();

        boolean isDeletedid = false;
        try {
          isDeletedid=FluggasBlockAdministrationService.deleteByExample(idList);
            commonResult.setIsSuccess(isDeletedid);
        } catch (Exception e) {
            logger.error("用户配置删除异常",e);
        }

        return commonResult;
    }

    @Override
    public CommonResult insertFluggasBlockAdministration(FluggasBlockAdministration bean) {
        CommonResult commonResult = new CommonResult();
        try {
            FluggasBlockAdministration fluggas = FluggasBlockAdministrationService.selectByblockId(bean.getBlockId());
            if (fluggas!=null) {
                commonResult.setResultMsg("区块已存在");
            } else {
                if(FluggasBlockAdministrationService.insertDate(bean))
                {
                        commonResult.setIsSuccess(true);
                }
                else
                {
                    commonResult.setResultMsg("插入配置信息失败");
                }
            }
        } catch (Exception e) {
            logger.error("废气配置添加异常", e);
        }
        return commonResult;
    }

    @Override
    public CommonResult addFiled(String filed, String type) {
        CommonResult commonResult = new CommonResult();
        try {
            if(FluggasBlockAdministrationService.addFiled(filed, type))
            {
                commonResult.setIsSuccess(true);
            } else
            {
                commonResult.setIsSuccess(false);
                commonResult.setResultMsg("添加字段失败");
            }
        } catch (Exception e) {
            logger.error("添加字段异常", e);
        }
        return commonResult;
    }


}
